#include "SDL_Joueur.h"

#include <time.h>
#include <stdio.h>
#define ABS(x) ((x)<(0)? (x)*-1 : (x))
#include <math.h>
#include <SDL/SDL.h>

#include "tir.h"
void initSDL_Joueur(SDL_Joueur* S)
{
    FILE* f;
    int i;
    char mot[255];

    S->timerech=S->timerreq=((float)clock())/CLOCKS_PER_SEC;
    S->j=NULL;
    f=fopen("data/config.ini","r");

    if (f==NULL)
    {
        f=fopen("data/config.ini","w");




        S->c=CLAVIER;
        S->key[HAUT]=SDLK_UP;
        S->key[BAS]=SDLK_DOWN;
        S->key[GAUCHE]=SDLK_LEFT;
        S->key[DROITE]=SDLK_RIGHT;
        S->key[ECHOBJ]=SDLK_q;
        S->key[RAMOBJ]=SDLK_e;
        S->key[TIRHAUT]=SDLK_w;
        S->key[TIRBAS]=SDLK_s;
        S->key[TIRGAUCHE]=SDLK_a;
        S->key[TIRDROITE]=SDLK_d;
        for (i=0;i<4;i++)
            S->axesjoy[i]=0;
    }
    else
    {
        while (strcmp("/*CONTROLES*/",mot))
            fscanf(f,"%s",mot);
        fscanf(f,"\nCONFIG=%s\n",mot);
        if (!strcmp(mot,"CLAVIER"))
        {
            S->c=CLAVIER;
            fscanf(f,"HAUT=%d\nBAS=%d\nGAUCHE=%d\nDROITE=%d\n"
                ,&S->key[HAUT],&S->key[BAS],&S->key[GAUCHE],&S->key[DROITE]);
            fscanf(f,"ECHOBJ=%d\nRAMOBJ=%d",&S->key[ECHOBJ],&S->key[RAMOBJ]);
            fscanf(f,"\nTIRHAUT=%d\nTIRBAS=%d\nTIRGAUCHE=%d\nTIRDROITE=%d",&S->key[TIRHAUT],&S->key[TIRBAS],&S->key[TIRGAUCHE],&S->key[TIRDROITE]);
        }
        else if (!strcmp(mot,"SOURIS"))
        {
            S->c=SOURIS;
            fscanf(f,"HAUT=%d\nBAS=%d\nGAUCHE=%d\nDROITE=%d\n"
                ,&S->key[HAUT],&S->key[BAS],&S->key[GAUCHE],&S->key[DROITE]);
            fscanf(f,"ECHOBJ=%d\nRAMOBJ=%d",&S->key[ECHOBJ],&S->key[RAMOBJ]);
            for (i=TIRHAUT;i<=TIRDROITE;i++)
                S->key[i]=0;
        }
        else if (!strcmp(mot,"MANETTE"))
        {
            S->c=MANETTE;
            fscanf(f,"HAUT=%d\nBAS=%d\nGAUCHE=%d\nDROITE=%d\n"
                ,&S->key[HAUT],&S->key[BAS],&S->key[GAUCHE],&S->key[DROITE]);
            fscanf(f,"ECHOBJ=%d\nRAMOBJ=%d",&S->key[ECHOBJ],&S->key[RAMOBJ]);
            fscanf(f,"\nTIRHAUT=%d\nTIRBAS=%d\nTIRGAUCHE=%d\nTIRDROITE=%d\n",&S->key[TIRHAUT],&S->key[TIRBAS],&S->key[TIRGAUCHE],&S->key[TIRDROITE]);
            fscanf(f,"X=%d\nY=%d\nXTIR=%d\nYTIR=%d\n",&S->axesjoy[0],&S->axesjoy[1],&S->axesjoy[2],&S->axesjoy[3]);
            if (SDL_NumJoysticks()>0)
            {
                if (SDL_JoystickOpened(0))
                    S->j=SDL_JoystickOpen(1);
                else
                    S->j=SDL_JoystickOpen(0);
            }
            if (!S->j)
                exit(EXIT_FAILURE);

        }
        else
            exit(EXIT_FAILURE);
    }
    SDL_ShowCursor(SDL_DISABLE);
    fclose(f);
}

int evenementhautSDL_Joueur(const SDL_Joueur* S, const Uint8 * keystates)
{
    if (S->c==MANETTE)
    {
        if (SDL_JoystickGetAxis(S->j,S->axesjoy[1])<-32768/3)
            return 1;
        else
            return 0;
    }
    return keystates[S->key[HAUT]];
}

int evenementbasSDL_Joueur(const SDL_Joueur* S, const Uint8 * keystates)
{
    if (S->c==MANETTE)
    {
        if (SDL_JoystickGetAxis(S->j,S->axesjoy[1])>32768/3)
            return 1;
        else
            return 0;
    }
    return keystates[S->key[BAS]];
}

int evenementgaucheSDL_Joueur(const SDL_Joueur* S, const Uint8 * keystates)
{
    if (S->c==MANETTE)
    {
        if (SDL_JoystickGetAxis(S->j,S->axesjoy[0])<-32768/3)
            return 1;
        else
            return 0;
    }
    return keystates[S->key[GAUCHE]];
}

int evenementdoiteSDL_Joueur(const SDL_Joueur* S, const Uint8 * keystates)
{
    if (S->c==MANETTE)
    {
        if (SDL_JoystickGetAxis(S->j,S->axesjoy[0])>32768/3)
            return 1;
        else
            return 0;
    }
    return keystates[S->key[DROITE]];
}

int evenementtirSDL_Joueur(const SDL_Joueur* S,const SDL_Event * event, const Uint8 * keystates,float * angle,int x, int y)
{
    int xeff,yeff,compt=0;
    if (S->c==SOURIS)
    {
        if (SDL_GetMouseState(&xeff,&yeff)&SDL_BUTTON_LEFT)
        {
            *angle=atan2((double)yeff-y,(double)xeff-x);
            return 1;
        }
    }
    if (S->c==CLAVIER)
    {
        *angle=0;

        if (((keystates[S->key[TIRHAUT]])&&(keystates[S->key[TIRBAS]]))
        ||((keystates[S->key[TIRGAUCHE]])&&(keystates[S->key[TIRDROITE]])))
            return 0;


        if (keystates[S->key[TIRHAUT]])
        {
            *angle-=M_PI/2.0;
            compt++;
        }
        if (keystates[S->key[TIRBAS]])
        {
            *angle+=M_PI/2.0;
            compt++;
        }
        if (keystates[S->key[TIRGAUCHE]])
        {
            *angle+=((*angle>0)? M_PI : -M_PI);
            compt++;
        }
        if (keystates[S->key[TIRDROITE]])
            compt++;


        if (compt==1)
            return 1;
        if (compt==2)
        {
            *angle/=2;
            return 1;
        }
        else
            return 0;
    }
    if (S->c==MANETTE)
    {
        xeff=SDL_JoystickGetAxis(S->j,S->axesjoy[2]);
        yeff=SDL_JoystickGetAxis(S->j,S->axesjoy[3]);
        if (module(0,0,xeff,yeff)>module(0,0,32768,32768)/2)
        {
            *angle=atan2((double)yeff,(double)xeff);
            return 1;
        }
    }
    return 0;
}

int evenementechangobjetSDL_Joueur(SDL_Joueur* S,const SDL_Event * event)
{
    float t=((float)clock())/CLOCKS_PER_SEC;
    if (t-S->timerech>0.4)
    {
        if (S->c==MANETTE)
        {
            if (SDL_JoystickGetButton(S->j,0))
            {
                S->timerech=t;
                return 1;
            }
        }
        else
            if (t-S->timerech>0.2 && event->key.keysym.sym==S->key[ECHOBJ]&&event->type==SDL_KEYDOWN)
            {
                S->timerech=t;
                return 1;
            }
    }
    return 0;
}

int evenementrammobjetSDL_Joueur(SDL_Joueur* S,const SDL_Event * event)
{
    float t=((float)clock())/CLOCKS_PER_SEC;
    if (t-S->timerreq>0.4)
    {
        if (S->c==MANETTE)
        {
            if (SDL_JoystickGetButton(S->j,2))
            {
                S->timerreq=t;
                return 1;
            }
        }
        else
            if (event->key.keysym.sym==S->key[RAMOBJ] && event->type==SDL_KEYDOWN)
            {
                S->timerreq=t;
                return 1;
            }
    }
    return 0;
}

void sauvegarderSDL_Joueur(const SDL_Joueur* S)
{
    FILE* f=fopen("data/config.ini","w");
    if (S->c==CLAVIER)
        fprintf(f,"CONFIG=CLAVIER\n");
    if (S->c==MANETTE)
        fprintf(f,"CONFIG=MANETTE\n");
    if (S->c==SOURIS)
        fprintf(f,"CONFIG=SOURIS\n");

    fprintf(f,"HAUT=%d\nBAS=%d\nGAUCHE=%d\nDROITE=%d\n"
                ,S->key[0],S->key[1],S->key[2],S->key[3]);
    fprintf(f,"ECHOBJ=%d\nRAMOBJ=%d",S->key[4],S->key[5]);
    fprintf(f,"\nTIRHAUT=%d\nTIRBAS=%d\nTIRGAUCHE=%d\nTIRDROITE=%d\n"
                ,S->key[6],S->key[7],S->key[8],S->key[9]);
    fprintf(f,"X=%d\nY=%d\nXTIR=%d\nYTIR=%d\n",S->axesjoy[0],S->axesjoy[1],S->axesjoy[2],S->axesjoy[3]);
    fclose(f);
}

void detruireSDL_Joueur(SDL_Joueur* S)
{
    if (S->j)
        SDL_JoystickClose(S->j);
}
